package com.tianhui.consignortwofei.pattern.orderOprate.impl;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.fgs.common.utils.ToastUtil;
import com.fgs.common.widget.dialog.OperateTipDialog;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.mvp.model.enty.OrderOperateInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.MainActivity;
import com.tianhui.consignortwofei.pattern.orderOprate.IOrderOperate;
import com.tianhui.consignortwofei.pattern.orderOprate.OrderOperateCallback;
import com.tianhui.consignortwofei.pattern.orderOprate.OrderRequestOperate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fgs
 * @date 2020/9/2
 * @description 终止单据(货源单)
 **/
public class TerminateSourceOperate implements IOrderOperate {
    @Override
    public void orderOperate(Context context, OrderOperateInfo operateInfo) {

////        司机取消/运输/装车/收货/已计算/终止/已完成过程中不可进行终止!
//        int status = operateInfo.orderInfo.isstate;
//        if (status == OrderStatus.CANCELING ||
//                status == OrderStatus.CANCELED ||
//                status == OrderStatus.IN_TRANSIT ||
//                status == OrderStatus.LOADING_CONFIRM ||
//                status == OrderStatus.DELIVER_CONFIRM ||
//                status == OrderStatus.CALCULATED ||
//                status == OrderStatus.TERMINATION ||
//                status == OrderStatus.PAYMENT ||
//                status == OrderStatus.COMPLETE
//        ) {
//            ToastUtil.showShortToast("司机取消/运输/装车/收货/已计算/终止/已完成过程中不可进行终止!");
//            return;
//        }


        new OperateTipDialog.Builder(context)
                .title("请输入原因")
                .input(new OperateTipDialog.InputCallback() {
                    @Override
                    public void onInput(@NonNull OperateTipDialog dialog, CharSequence input) {
                    }
                })
                .positiveText("确定")
                .negativeText("取消")
                .onPositive(new OperateTipDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull OperateTipDialog dialog) {
                        String reason = dialog.getInputContent();
                        if (TextUtils.isEmpty(reason)) {
                            ToastUtil.showShortToast("请输入终止原因");
                            return;
                        }
                        dialog.dismiss();
                        Map<String, String> map = new HashMap<>();
                        map.put("billno", operateInfo.orderInfo.billno);
                        map.put("remark", reason);
                        map.put("isstate", "11");
                        new OrderRequestOperate().orderOperateRequest(context, map,operateInfo.orderInfo.dingJinType ,
                                new OrderOperateCallback() {
                            @Override
                            public void operateCallback() {
                                showTerminateResultDialog(context, "单据终止成功", operateInfo);
                                EventBusManage.refreshOrderList();
                                EventBusManage.activityFinish();
                            }

                            @Override
                            public void onFailed(String message) {

                            }
                        });
                    }
                })
                .show();
    }

    private void showTerminateResultDialog(Context context, String message, OrderOperateInfo operateInfo) {
        new OperateTipDialog.Builder(context)
                .title("提示")
                .content(message)
                .isCancelable(false)
                .positiveText("返回列表")
                .onPositive(new OperateTipDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull OperateTipDialog dialog) {
                        dialog.dismiss();
                        Intent intent = new Intent(context, MainActivity.class);
//                        Intent intent = new Intent(context, OrderListActivity.class);
//                        Bundle bundle = new Bundle();
//                        bundle.putInt(KeyValue.KEY_VALUE, operateInfo.type);
//                        intent.putExtras(bundle);
                        context.startActivity(intent);
                    }
                })
                .show();
    }
//    @Override
//    public void orderOperate(Context context, OrderOperateInfo operateInfo, OrderOperateCallback orderOperateCallback) {
//
//    }
}
